#include "freqtocentwidget.h"
#include "ui_freqtocentwidget.h"

freqToCentWidget::freqToCentWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::freqToCentWidget)
{
    ui->setupUi(this);

    setupConnections();

    updateDifference();

}

freqToCentWidget::~freqToCentWidget()
{
    delete ui;
}

void freqToCentWidget::setupConnections(void)
{
    this->connect(ui->freq1_spin, SIGNAL(valueChanged(double)), this, SLOT(onFirstFreqChanged()));
    this->connect(ui->freq2_spin, SIGNAL(valueChanged(double)), this, SLOT(onSecondFreqChanged()));
    //this->connect(ui->cents_spin, SIGNAL(valueChanged(int)), this, SLOT(onCentsChanged(int)));
}

void freqToCentWidget::updateDifference()
{
    // Compute new difference (cents and freq)
    ui->freq_diff_spin->setValue(ui->freq2_spin->value() - ui->freq1_spin->value());
    int cents = centsDiff(ui->freq1_spin->value(), ui->freq2_spin->value());
    ui->cents_spin->setValue(cents);

    // Color spinbox
    //int val = ui->cents_spin->value();
    int val = qAbs(cents);
    QPalette* palette = new QPalette();
    if ( val <= 50 ) {
        palette->setColor(QPalette::Background ,Qt::green);
    } else if ( val > 50 && val <= 100 ) {
        palette->setColor(QPalette::Background ,Qt::yellow);
    } else if ( val > 100 ) {
        palette->setColor(QPalette::Background ,Qt::red);
    }
    ui->cents_spin->setPalette(*palette);
}

//void freqToCentWidget::onCentsChanged(int val)
//{
//
//}

void freqToCentWidget::onFirstFreqChanged(void)
{
    updateDifference();
}

void freqToCentWidget::onSecondFreqChanged(void)
{
    updateDifference();
}
